﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Scheduler;

namespace Win7RovMobileApp
{
    public partial class CheckingMenu : PhoneApplicationPage
    {
        DateTime? ValabilityExpireDate = null;
        DateTime? ValabilityStartDate = null;

        public CheckingMenu()
        {
            InitializeComponent();
        }


        void RenderTimeElapsing(object sender, EventArgs e)
        {
            TimeSpan expireMoment = ((DateTime)ValabilityExpireDate).Subtract((DateTime)ValabilityStartDate);
            int months = ((DateTime)ValabilityExpireDate).Month - ((DateTime)ValabilityStartDate).Month;
            int days = ((DateTime)ValabilityExpireDate).Day - ((DateTime)ValabilityStartDate).Day;

            if ((DateTime)ValabilityStartDate < DateTime.Now)
            {
                expireMoment = ((DateTime)ValabilityExpireDate).Subtract(DateTime.Now);
                months = ((DateTime)ValabilityExpireDate).Month - DateTime.Now.Month + 12 * (((DateTime)ValabilityExpireDate).Year - DateTime.Now.Year);
                days = ((DateTime)ValabilityExpireDate).Day - DateTime.Now.Day;
            }
            
            // update the time
            Month1.AnimateTo(months / 10);
            Month2.AnimateTo(months % 10);

            Day1.AnimateTo(days / 10);
            Day2.AnimateTo(days % 10);

            Hour1.AnimateTo(expireMoment.Hours / 10);
            Hour2.AnimateTo(expireMoment.Hours % 10);

            Minute1.AnimateTo(expireMoment.Minutes / 10);
            Minute2.AnimateTo(expireMoment.Minutes % 10);

            Second1.AnimateTo(expireMoment.Seconds / 10);
            Second2.AnimateTo(expireMoment.Seconds % 10);
        }


        private void CheckCarPlates_Click(object sender, EventArgs e)
        {
            RovinieteServiceReference.Windows_Phone_7_ServiceClient client = new RovinieteServiceReference.Windows_Phone_7_ServiceClient();
            client.GetAvailabilityCompleted += new EventHandler<RovinieteServiceReference.GetAvailabilityCompletedEventArgs>(client_GetAvailabilityCompleted);

            if (String.IsNullOrWhiteSpace(CarPlateTextBox.Text.Trim()))
            {
                MessageBox.Show("Va rugam completati mai intai numarul de inmatriculare!");
                return;
            }

            client.GetAvailabilityAsync(CarPlateTextBox.Text.Trim());

        }


        void client_GetAvailabilityCompleted(object sender, RovinieteServiceReference.GetAvailabilityCompletedEventArgs e)
        {
            ValabilityStartDate = null;
            ValabilityExpireDate = null;
            if (e.Error == null)
            {
                ValabilityStartDate = e.Result[0];
                ValabilityExpireDate = e.Result[1];
            }
            
            if (ValabilityExpireDate == null)
            {
                MessageBox.Show("Momentan NU exista o rovinieta pentru numarul de inmatriculare introdus!");
                return;
            }

            CompositionTarget.Rendering += new EventHandler(RenderTimeElapsing);

            AlarmImage.Visibility = AlarmTextBlock.Visibility = ValabilityStartDate_TextBox.Visibility = ValabilityStartDate_TextBlock.Visibility = System.Windows.Visibility.Visible;
            ValabilityStartDate_TextBox.Text = (ValabilityStartDate != null) ? ValabilityStartDate.Value.ToString() : String.Empty;
            AlarmTextBlock.Text = "Seteaza alarma pentru " + CarPlateTextBox.Text.Trim();
            AlarmRemoveTextBlock.Text = "Sterge alarma pentru " + CarPlateTextBox.Text.Trim();

            // if there is already an alarm with this name
            if (ScheduledActionService.Find(CarPlateTextBox.Text.Trim()) != null)
            {
                AlarmRemoveImage.Visibility = AlarmRemoveTextBlock.Visibility = System.Windows.Visibility.Visible;
                AlarmImage.Visibility = AlarmTextBlock.Visibility = System.Windows.Visibility.Collapsed;
            }
        }


        private void CancelChecking_Click(object sender, EventArgs e)
        {
            AlarmImage.Visibility = AlarmTextBlock.Visibility = AlarmRemoveImage.Visibility = AlarmRemoveTextBlock.Visibility = System.Windows.Visibility.Collapsed;
            ValabilityStartDate_TextBox.Visibility = ValabilityStartDate_TextBlock.Visibility = System.Windows.Visibility.Collapsed;
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }


        private void SetAlarm_Click(object sender, MouseButtonEventArgs e)
        {
            Alarm alarm = new Alarm(CarPlateTextBox.Text.Trim());
            alarm.BeginTime = DateTime.Now.AddSeconds(5);   //TEST for now... TODO - set "ValabilityExpireDate" minus 1 Day
            alarm.Content = "Rovinieta pentru numarul de inmatriculare " + CarPlateTextBox.Text.Trim() + " va expira maine!";
            alarm.Sound = new Uri("alarm.mp3", UriKind.RelativeOrAbsolute);
            ScheduledActionService.Add(alarm);

            MessageBox.Show("Alarma a fost setata cu succes!");

            AlarmImage.Visibility = AlarmTextBlock.Visibility = System.Windows.Visibility.Collapsed;
            AlarmRemoveImage.Visibility = AlarmRemoveTextBlock.Visibility = System.Windows.Visibility.Visible;
        }


        private void RemoveAlarm_Click(object sender, MouseButtonEventArgs e)
        {
            // alarm name will be exactly the car plate number            
            ScheduledActionService.Remove(CarPlateTextBox.Text.Trim());
            MessageBox.Show("Alarma a fost inlaturata cu succes!");

            AlarmImage.Visibility = AlarmTextBlock.Visibility = System.Windows.Visibility.Visible;
            AlarmRemoveImage.Visibility = AlarmRemoveTextBlock.Visibility = System.Windows.Visibility.Collapsed;
        }

    }
}